package com.bjkcst.baselib.utils;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;

import java.io.File;

public class UsbManger {
    private UsbListener listener;
    private String logPath;
    private String usbPath;
    private static UsbManger usbManger;

    public static UsbManger getInstance() {
        if (usbManger == null) {
            synchronized (SharedPreferences.class) {
                if (usbManger == null) {
                    usbManger = new UsbManger();
                }
            }
        }
        return usbManger;
    }

    //usb挂载
    public void usbMount(Uri uri) {
        if (null == uri) {
            return;
        }
        if (null != listener) {
            listener.onUsbInsert();
        }
        if (!TextUtils.isEmpty(logPath)) {
            String filePath = uri.getPath();
            if (TextUtils.isEmpty(usbPath)) {
                usbPath = "kingkong";
            }
            File logFile = new File(logPath);
            if (!logFile.exists()) {
                return;
            }
            //执行log文件拷贝操作
            //File usbFile = new File(filePath + File.separator + usbPath);
            File usbFile = new File(filePath + File.separator + usbPath);
            if (!usbFile.exists()) {
                usbFile.mkdirs();
            }

            // ToastUtil.showToast(context, "检测到U盘,开始拷贝文件！");
            KcstThreadPool.getInstance().singleExecute(new Runnable() {
                @Override
                public void run() {
                    if (FileUtils.isFile(logFile)) {
                        String path = usbFile.getAbsolutePath() + File.separator + logFile.getName();
                        if (null != listener) {
                            listener.onCopyStar();
                        }
                        if (FileUtils.copyFile(logPath, path)) {
                            if (null != listener) {
                                listener.onCopyOver();
                            }
                        } else {
                            if (null != listener) {
                                listener.onCopyError();
                            }
                        }

                    } else if (FileUtils.isDir(logFile)) {
                        //log路径是文件夹
                        if (null != listener) {
                            listener.onCopyStar();
                        }

                        if (FileUtils.copyFolder(logPath, usbFile.getAbsolutePath())) {
                            if (null != listener) {
                                listener.onCopyOver();
                            }
                        } else {
                            if (null != listener) {
                                listener.onCopyError();
                            }
                        }
                    }
                }
            });
        }
    }

    //usb移除
    public void usbRemove() {
        if (null != listener) {
            listener.onUsbRemoval();
        }
    }


    public UsbManger setListener(UsbListener listener) {
        this.listener = listener;
        return this;
    }

    public UsbManger setLogPath(String logPath) {
        this.logPath = logPath;
        return this;
    }

    public UsbManger setUsbPath(String usbPath) {
        this.usbPath = usbPath;
        return this;
    }

    public interface UsbListener {
        void onUsbInsert();

        void onUsbRemoval();

        void onCopyStar();

        void onCopyOver();

        void onCopyError();
    }
}
